package renameFile;

import java.io.File;

public class RenameFiles {

    public static void main(String[] args) {
        String mainName = "/Volumes/COCO/Music/MapleStory";
        cycleRename(mainName);
    }


    static void cycleRename(String folderName) {
        File folder = new File(folderName);

        //获取目录下的所有文件列表，并遍历处理
        if (folder.isFile()) {
            renameFile(folder, folder.getPath());
            return;
        }
        File[] listOfFiles = folder.listFiles();
        if (listOfFiles == null || listOfFiles.length == 0) {
            return;
        }
        for (File file : listOfFiles) {
            if (file.isFile()) {
                renameFile(file, folder.getPath());
            } else {
                cycleRename(file.getPath());
            }
        }
    }


    static void renameFile(File file, String path) {
        String fileName = file.getName();
        String newName = fileName.replaceFirst("Asteria,", " ");
        System.out.println("新文件名:" + newName);
        File newFile = new File(path + "/" + newName);
        if (file.renameTo(newFile)) {
            System.out.println("文件重命名成功！旧文件名：" + fileName + "，新文件名：" + newName);
        } else {
            System.out.println("重命名失败。");
        }

    }
}